***********************************************************************************
***	Replication file for:                                                     	***
*** Braun, S. T. and Stuhler, J. (2024). The Economic Consequences of 			***
***	Being Widowed by War: A Life-Cycle Perspective.	     						***
***																				***
***	Journal of Public Economics                                                 ***
***   							                                                ***
*** Script:		_2a-ghs-1-prep-cross-section-p1.do				   			 	***	
*** Purpose:	data preparation, cross-sectional data GHS-1, part 1			***
***																				***
***********************************************************************************

***
* Note: Time variable t counts months since 01/1900 
***


/*
1001 "Leiblicher Vater der Indexperson (Großvater)"
1002 "Leibliche Mutter der Indexperson (Großmutter)"

2001 "Indexperson"

2002 "Partner 1 der Indexperson (jetziger/letzter Partner)"
2003 "Partner 2 der Indexperson"
2004 "Partner 3 der Indexperson"
2005 "Partner 4 der Indexperson"

2011 "Geschwister 1 der Indexperson"
2012 "Geschwister 2 der Indexperson"
2013 "Geschwister 3 der Indexperson"
2014 "Geschwister 4 der Indexperson"
2015 "Geschwister 5 der Indexperson"
2016 "Geschwister 6 der Indexperson"
2017 "Geschwister 7 der Indexperson"
2018 "Geschwister 8 der Indexperson"
2019 "Geschwister 9 der Indexperson"
2020 "Geschwister 10 der Indexperson"
2021 "Geschwister 11 der Indexperson"
2022 "Geschwister 12 der Indexperson"
*/

/*
1001 "Biological father of index person (grandfather)"
1002 "Biological mother of index person (grandmother)"

2001 "Index person"

2002 "Partner 1 of the index person (current/last partner)"
2003 "Partner 2 of the index person"
2004 "Partner 3 of the index person"
2005 "Partner 4 of the index person"

2011 "Sibling 1 of index person"
2012 "Sibling 2 of index person"
2013 "Sibling 3 of index person"
2014 "Sibling 4 of index person"
2015 "Sibling 5 of index person"
2016 "Sibling 6 of index person"
2017 "Sibling 7 of index person"
2018 "Sibling 8 of index person"
2019 "Sibling 9 of index person"
2020 "Sibling 10 of index person"
2021 "Sibling 11 of index person"
2022 "Sibling 12 of index person"
*/

**************************************************
* Cross-sectional data for index person
**************************************************

use "$widowsghs/data/LV1/REC1.DTA", clear

* Schooling/schooling history

tab A05 
recode A05 96/99 = .
recode A05 0 = .
rename A05 einschulung_jahr
replace einschulung_jahr = 1900 + einschulung_jahr
label var einschulung_jahr "Year of school enrollment"

tab A21
recode A21 96/99 = .
recode A21 0 = .
rename A21 schulaustritt_monat 
replace schulaustritt_monat = schulaustritt_monat - 20 if (schulaustritt_monat >= 21 & schulaustritt_monat <= 32)
label var schulaustritt_monat "Month of leaving school"

tab A22
recode A22 96/99 = .
recode A22 0 = .
rename A22 schulaustritt_jahr
replace schulaustritt_jahr = 1900 + schulaustritt_jahr
label var schulaustritt_jahr "Year of leaving school" 

tab A23
recode A23 8/9 = .
rename A23 schulabschluss
label var schulabschluss "Highest school-leaving qualification"

* Military / civil service

tab A27
recode A27 6/9 = .
rename A27 militaer_zivil_dienst
label var militaer_zivil_dienst "Did military or civilian service?"

tab A29
recode A29 96/99 = .
rename A29 dienst_beginn_monat
replace dienst_beginn_monat = dienst_beginn_monat - 20 if (dienst_beginn_monat>= 21 & dienst_beginn_monat <= 32)
label var dienst_beginn_monat "Start of military or civilian service, month"

tab A30
recode A30 96/99 = .
recode A30 0 = .
rename A30 dienst_beginn_jahr
replace dienst_beginn_jahr = 1900 + dienst_beginn_jahr 
label var dienst_beginn_jahr "Start of military or civilian service, year"


tab A31
recode A31 96/99 = .
rename A31 dienst_ende_monat
replace dienst_ende_monat = dienst_ende_monat - 20 if (dienst_ende_monat>= 21 & dienst_ende_monat <= 32)
label var dienst_ende_monat "End of military or civilian service, month"

tab A32
recode A32 96/99 = .
recode A32 0 = .
rename A32 dienst_ende_jahr
replace dienst_ende_jahr = 1900 + dienst_ende_jahr 
label var dienst_ende_jahr "End of military or civilian service, year"


* Socio-demographic characteristics

tab A140
recode A140 96/99 = .
rename A140 geburtsmonat
label var geburtsmonat "Month of birth"

tab A141
recode A141 96/99 = .
rename A141 geburtsjahr
replace geburtsjahr = geburtsjahr + 1900
label var geburtsjahr "Year of birth"

tab A206
recode A206 6/9 = .
rename A206 geschlecht
label var geschlecht "Gender"

tab A207
recode A207 96/99 = .
rename A207 familienstand
label var familienstand "Marital status"

tab A333
rename A333 geburtsdatum_monate
label var geburtsdatum_monate "Date of birth in months since 1900"

* Index for generations

gen index = 2001
label var index "Generationenindex"

* Save data

keep A01 geburt* geschlecht familienstand index einschulung* schulaustritt* dienst* mili* schulabschluss

save "$widowsghs/processed/intermediate/LVIPa_CS_I2001_REC1.dta", replace

**********************************
* Number of children
**********************************

use "$widowsghs/data/LV1/KINDER.dta", clear

bysort A01: egen anz_kinder = max(P01)
label var anz_kinder "Number of children"

keep if P01 == 1

keep A01 anz_kinder

save "$widowsghs/processed/intermediate/LVIPa_CS_I2001_anzkinder.dta", replace

**********************************
* Number of siblings
**********************************

use "$widowsghs/data/LV1/GESCHWIS.dta", clear

bysort A01: egen anz_geschwister = max(D01)
label var anz_geschwister "Number of siblings"

keep if D01 == 1

keep A01 anz_*

save "$widowsghs/processed/intermediate/LVIPa_CS_I2001_anzgeschw.dta", replace



**********************************
* Information from REC6 SCHOOL
**********************************

use "$widowsghs/data/LV1/SCHOOL.dta", clear

gen help = 0
replace help = 1 if F07 == 31 | F07 == 42
bysort A01: egen lehre_gewerblich = max(help)
drop help
label var lehre_gewerblich "F07: Industrial apprenticeship"

gen help = 0
replace help = 1 if F07 == 32
bysort A01: egen lehre_landwirtschaft = max(help)
drop help
label var lehre_landwirtschaft "F07: Apprenticeship in agriculture or home economics"

gen help = 0
replace help = 1 if F07 == 33 | F07 == 43
bysort A01: egen lehre_kaufm = max(help)
drop help
label var lehre_kaufm "F07: Commercial apprenticeship, administrative apprenticeship"

gen help = 0
replace help = 1 if F07 == 21 | F07 == 22
bysort A01: egen berufsfachschule = max(help)
drop help
label var berufsfachschule "F07: Completion of a vocational school/technical college"

gen help = 0
replace help = 1 if F07 == 23 | F07 == 24
bysort A01: egen fachhochschulabschluss = max(help)
drop help
label var fachhochschulabschluss "F07: Degree from a university of applied sciences/vocational academy"

gen help = 0
replace help = 1 if F07 == 25 | F07 == 26 | F07 == 27 
bysort A01: egen uniabschluss = max(help)
drop help
label var uniabschluss "F07: University degree (Staatsexamen/Diplom/Magister)"

gen help = 0
replace help = 1 if F07 == 28
bysort A01: egen promotion = max(help)
drop help
label var promotion "F07: PhD"

gen help = 0
replace help = 1 if F07 == 41
bysort A01: egen einf_dienst = max(help)
drop help
label var einf_dienst "F07: Degree for einfacher Dienst"

gen help = 0
replace help = 1 if F07 == 44 | F07 == 45
bysort A01: egen mitt_dienst = max(help)
drop help
label var mitt_dienst "F07: Degree for mittlerer Dienst"

gen help = 0
replace help = 1 if F07 == 46
bysort A01: egen geh_dienst = max(help)
drop help
label var geh_dienst "F07: Degree for gehobener Dienst"

gen help = 0
replace help = 1 if F07 == 47
bysort A01: egen hoh_dienst = max(help)
drop help
label var hoh_dienst "F07: Degree for hoher Dienst"

gen help = 0
replace help = 1 if F07 == 30 | F07 == 40
bysort A01: egen sonst_abschluss = max(help)
drop help
label var hoh_dienst "F07: Other degrees"

gen help = 0
replace help = 1 if F07 == 51
bysort A01: egen meisterabschluss = max(help)
drop help
label var meisterabschluss "F07: Meister/Master"

keep if F01 == 1
drop F0* CF03 CF05


* Generate variable on vocational training/education analogous to coding for parents 
/*
(1) GEWERBLICHE LEHRE
(2) KAUFMŎNISCHE LEHRE/VERWALTUNGSLEHRE
(3) HAUSWIRTSCHAFTS-/LANDWIRTSCHAFTSLEHRE
(4) BERUFSFACHSCHULE
(5) PRAKTIKUM/VOLONTARIAT
(6) MEISTER
(7) FACHHOCHSCHULE
(8) UNIVERSITAET
(9) KEINE AUSBILDUNG/OHNE ABSCHLUSS
*/
/*
(1) INDUSTRIAL APPRENTICESHIP (2) COMMERCIAL APPRENTICESHIP/ADMINISTRATIVE APPRENTICESHIP (3) HOME ECONOMICS/AGRICULTURAL APPRENTICESHIP (4) VOCATIONAL COLLEGE (5) INTERNSHIP/VOLUNTEERING (6) MASTER CRAFTSMAN (7) UNIVERSITY OF APPLIED SCIENCES (8) UNIVERSITY (9) NO TRAINING/NO DEGREE */


gen berufsausbildung = 9
replace berufsausbildung = 3 if lehre_landwirtschaft == 1
replace berufsausbildung = 1 if lehre_gewerblich == 1
replace berufsausbildung = 4 if berufsfachschule == 1
replace berufsausbildung = 2 if lehre_kaufm == 1
replace berufsausbildung = 6 if meisterabschluss == 1
replace berufsausbildung = 7 if fachhochschulabschluss == 1
replace berufsausbildung = 8 if uniabschluss == 1 | promotion == 1
replace berufsausbildung = 0 if berufsausbildung == 9 & (einf_dienst == 1 | mitt_dienst == 1 | geh_dienst == 1 | hoh_dienst == 1 | sonst_abschluss == 1)


label define berufsausbl 0 "SONSTIGER ABSCH. (OEFF. DIENST)" 1 "GEWERBLICHE LEHRE" 2 "KAUFMAENNISCHE LEHRE/VERWALTUNGSLEHRE" 3 "HAUSWIRTSCHAFTS-/LANDWIRTSCHAFTSLEHRE" 4 "BERUFSFACHSCHULE" 5 "PRAKTIKUM/VOLONTARIAT" 6 "MEISTER" 7 "FACHHOCHSCHULE" 8 "UNIVERSITAET" 9 "KEINE AUSBILDUNG/OHNE ABSCHLUSS"
label values berufsausbildung berufsausbl
label var berufsausbildung "AAB11: Highest educational qualification"

* Save data

keep A01 berufsausbildung 

compress

sort A01
save "$widowsghs/processed/intermediate/LVIPa_CS_I2001_REC6.dta", replace

**********************************
* Information from REC7 EIGJOB
**********************************

use "$widowsghs/data/LV1/EIGJOB.dta", clear
drop if G01 == .

* Recoding Prestige
* Code prestige as missing if occupation is missing, still in training or retired/retired
replace G04 = . if (G03 == 8 | G03 == 9 | G03 == 5 | G03 == 7) 
* Code prestige as 0 if jobseeker or non-employed housewife
replace G04 = 0 if G03 == 3 | G03 == 6
* Replace prestige value of 0 with missing if profession is indeterminable or impermissibly described
recode G04 0 = . if G03 == 4

* Prestige in first profession, highest prestige value acquired in the course of employment
gen help = .
replace help = G04 if G01 == 1
bysort A01: egen beruf_erster_prestige = mean(help)
recode beruf_erster_prestige . = -9
label var beruf_erster_prestige "Prestige in the first profession practiced"


bysort A01: egen beruf_prestige_max = max(G04)
recode beruf_prestige_max . = -9
label var beruf_prestige_max "ABG02: Highest prestige value achieved in the course of career"


* Save data

keep if G01 == 1
keep A01 beruf*

compress

sort A01
save "$widowsghs/processed/intermediate/LVIPa_CS_I2001_REC7.dta", replace


*************************************************
* Merge the data for the index person
*************************************************

merge m:m A01 using "$widowsghs/processed/intermediate/LVIPa_CS_I2001_REC1.dta"
drop _merge

merge m:m A01 using "$widowsghs/processed/intermediate/LVIPa_CS_I2001_REC6.dta"
drop _merge

merge m:m A01 using "$widowsghs/processed/intermediate/LVIPa_CS_I2001_anzkinder.dta"
drop _merge

merge m:m A01 using "$widowsghs/processed/intermediate/LVIPa_CS_I2001_anzgeschw.dta"
drop _merge

recode berufsausbildung . = 9

* Recoding: For never-employed persons, the employment history is missing, variables are set to . when merging => code to "non-employed"

recode beruf_erster_prestige . = 0
recode beruf_erster_prestige -9 = .

recode beruf_prestige_max . = 0
recode beruf_prestige_max -9 = .

* Recoding: For Respondents without entry for siblings/children set number of siblings/children to zero

recode anz_kinder . = 0
recode anz_geschwister . = 0


sort A01

save "$widowsghs/processed/intermediate/LVIPa_CS_I2001.dta", replace



***********************************************
* Spouse of the index person
***********************************************

/*
* Last spouse

Available for marital status == 2/3/4/5/6/7

A207, F.500 ROSA-BLAU FAMILIENSTAND
(1) VERHEIRATET, LEBE MIT MEINEM EHEMANN ZUS
(2) VERHEIR.,LEBE VON EHEMANN GETRENNT,WOHNE MIT NEUEM PARTNER
(3) VERHEIR.,LEBE VON EHEMANN GETRENNT,WOHNE OHNE NEUEM PARTNER
(4) GESCHIEDEN, WOHNE OHNE PARTNER
(5) GESCHIEDEN, WOHNE MIT NEUEM PARTNER ZUSAMMEN
(6) VERWITWET, WOHNE OHNE PARTNER
(7) VERWITWET, WOHNE MIT PARTNER ZUSAMMEN
(8) LEDIG, WOHNE OHNE PARTNER
(9) LEDIG, WOHNE MIT MEINEM PARTNER ZUSAMMEN
(96) NICHT CODIERBAR
(97) VERWEIGERT
(98) WEISS NICHT
(99) KEINE ANGABE

* Existing information on occupation by marital status

(1)/(9) Occupation learned by partner in REC1; detailed employment history of current partner in JOBPART
(2)/(5)/(7) Occupation learned by current partner and spouse in REC1, last occupation of spouse before separation in REC1, detailed employment history of current partner in JOBPART
(3)/(4)/(6) Occupation learned by spouse and last occupation before separation in REC1

*/

* (1) VERHEIRATET, LEBE MIT MEINEM EHEMANN/MEINER EHEFRAU ZUSAMMEN
* (1) MARRIED, LIVING WITH MY HUSBAND/WIFE

use "$widowsghs/data/LV1/REC1.DTA", clear

keep if A207 == 1

expand 5
bysort A01: gen n = _n

gen index = 2001 + n

* Current partner

gen status_spouse = A237 if index == 2002
recode status_spouse 0 = .
recode status_spouse 6/9 = .
label var status_spouse "Status of the current partner"

tab A225
recode A225 96/99 = .
gen help = A225 if index == 2002
rename help heiratsjahr 

tab A238
recode A238 96/99 = .
gen help = A238 if index == 2002
rename help geburtsjahr

* School and education

tab A239
recode A239 8/9 = .
gen help = A239 if index == 2002
rename help schulabschluss

tab A240
recode A240 6/9 = .
gen help = A240 if index == 2002
rename help lehre_gewerblich

tab A241
recode A241 6/9 = .
gen help = A241 if index == 2002
rename help lehre_kaufm

tab A242
recode A242 6/9 = .
gen help = A242 if index == 2002
rename help lehre_landwirtschaft

tab A243
recode A243 6/9 = .
gen help = A243 if index == 2002
rename help berufsfachschule

tab A244
recode A244 6/9 = .
gen help = A244 if index == 2002
rename help praktikum

tab A245
recode A245 6/9 = .
gen help = A245 if index == 2002
rename help meisterabschluss

tab A246
recode A246 6/9 = .
gen help = A246 if index == 2002
rename help fachhochschulabschluss

tab A247
recode A247 6/9 = .
gen help = A247 if index == 2002
rename help uniabschluss

tab A248
recode A248 6/9 = .
gen help = A248 if index == 2002
rename help kein_ausbabschluss

* Learned profession & profession before marriage

tab A296 
recode A296 6/9 = .
gen help = A296 if index == 2002
rename help beruf_erlernt

tab A297
recode A297 96/99 = .
gen help = A297 if index == 2002
rename help beruf_erlernt_prestige 

tab A308
recode A308 6/9 = .
replace A308 = . if status_spouse != 3 & status_spouse != 4 /* Only defined for wife/female partner */  
gen help = A308 if index == 2002
rename help beruf_vehe	

tab A309
recode A309 96/99 = .
replace A309 = . if status_spouse != 3 & status_spouse != 4  /* Only defined for wife/female partner */  
gen help = A309 if index == 2002
rename help beruf_vehe_prestige	


/* Former spouses: Only data on the duration of the marriage is available here */

gen trennungsart = .
gen trennungsjahr = .


* 1. previous marriage

tab A228
recode A228 96/99 = .
replace heiratsjahr = A228 if index == 2003

tab A230
recode A230 96/99 = .
replace trennungsjahr = A230 if index == 2003

tab A231
recode A231 6/9 = .
replace trennungsart = A231 if index == 2003


* 2. previous marriage

tab A233
recode A233 96/99 = .
replace heiratsjahr = A233 if index == 2004

tab A235
recode A235 96/99 = .
replace trennungsjahr = A235 if index == 2004

tab A236
recode A236 6/9 = .
replace trennungsart = A236 if index == 2004


* 3. previous marriage

tab A199
recode A199 96/99 = .
replace heiratsjahr = A199 if index == 2005

tab A201
recode A201 96/99 = .
replace trennungsjahr = A201 if index == 2005

tab A202
recode A202 6/9 = .
replace trennungsart = A202 if index == 2005

recode trennungsjahr 0 = .
recode trennungsart 0 = .
recode heiratsjahr 0 = .


rename A01 help
drop A* n
rename help A01

save "$widowsghs/processed/intermediate/I2002+_Partner1_LV1.dta", replace

* (2) VERHEIR., LEBE VON EHEMANN GETRENNT, WOHNE MIT NEUER PARTNERIN
* (2) MARRIED, SEPARATED FROM HUSBAND, LIVING WITH NEW PARTNER

use "$widowsghs/data/LV1/REC1.DTA", clear

keep if A207 == 2

expand 5
bysort A01: gen n = _n

gen index = 2001 + n

* Current partner

gen status_spouse = A237 if index == 2002
recode status_spouse 0 = .
recode status_spouse 6/9 = .
label var status_spouse "Status of the current partner"

tab A330
recode A330 96/99 = .
gen help = A330 if index == 2002
rename help zusammenjahr 

tab A238
recode A238 96/99 = .
gen help = A238 if index == 2002
rename help geburtsjahr

tab A239
recode A239 8/9 = .
gen help = A239 if index == 2002
rename help schulabschluss

tab A240
recode A240 6/9 = .
gen help = A240 if index == 2002
rename help lehre_gewerblich

tab A241
recode A241 6/9 = .
gen help = A241 if index == 2002
rename help lehre_kaufm

tab A242
recode A242 6/9 = .
gen help = A242 if index == 2002
rename help lehre_landwirtschaft

tab A243
recode A243 6/9 = .
gen help = A243 if index == 2002
rename help berufsfachschule

tab A244
recode A244 6/9 = .
gen help = A244 if index == 2002
rename help praktikum

tab A245
recode A245 6/9 = .
gen help = A245 if index == 2002
rename help meisterabschluss

tab A246
recode A246 6/9 = .
gen help = A246 if index == 2002
rename help fachhochschulabschluss

tab A247
recode A247 6/9 = .
gen help = A247 if index == 2002
rename help uniabschluss

tab A248
recode A248 6/9 = .
gen help = A248 if index == 2002
rename help kein_ausbabschluss

tab A300 
recode A300 6/9 = .
gen help = A300 if index == 2002
rename help beruf_erlernt

tab A301
recode A301 96/99 = .
gen help = A301 if index == 2002
rename help beruf_erlernt_prestige 

tab A308
recode A308 6/9 = .
replace A308 = . if status_spouse != 3 & status_spouse != 4 /* Only defined for wife/female partner */  
gen help = A308 if index == 2002
rename help beruf_vehe	

tab A309
recode A309 96/99 = .
replace A309 = . if status_spouse != 3 & status_spouse != 4 /* Only defined for wife/female partner */ 
gen help = A309 if index == 2002
rename help beruf_vehe_prestige	

* Husband/wife

tab A210
recode A210 96/99 = .
gen help = A210 if index == 2003
rename help trennungsjahr

tab A211
recode A211 8/9 = .
replace geburtsjahr = A211 if index == 2003

tab A212
recode A212 8/9 = .
replace schulabschluss = A212 if index == 2003

tab A213
recode A213 6/9 = .
replace lehre_gewerblich = A213 if index == 2003

tab A214
recode A214 6/9 = .
replace lehre_kaufm = A214 if index == 2003

tab A215
recode A215 6/9 = .
replace lehre_landwirtschaft = A215 if index == 2003

tab A216
recode A216 6/9 = .
replace berufsfachschule = A216 if index == 2003

tab A217
recode A217 6/9 = .
replace praktikum = A217 if index == 2003

tab A218
recode A218 6/9 = .
replace meisterabschluss = A218 if index == 2003

tab A219
recode A219 6/9 = .
replace fachhochschulabschluss = A219 if index == 2003

tab A220
recode A220 6/9 = .
replace uniabschluss = A220 if index == 2003

tab A221
recode A221 6/9 = .
replace kein_ausbabschluss = A221 if index == 2003

gen heiratsjahr = .
tab A225
recode A225 96/99 = .
replace heiratsjahr = A225 if index == 2003

tab A296 
recode A296 6/9 = .
replace beruf_erlernt = A296 if index == 2003

tab A297
recode A297 96/99 = .
replace beruf_erlernt_prestige = A297 if index == 2003

tab A304
recode A304 6/9 = .
gen help = A304 if index == 2003
rename help beruf_vtrenn

tab A305
recode A305 96/99 = .
gen help = A305 if index == 2003
rename help beruf_vtrenn_prestige

/* Former spouses: Only data on the duration of the marriage is available here */

gen trennungsart = .

* 1. previous marriage

tab A228
recode A228 96/99 = .
replace heiratsjahr = A228 if index == 2004

tab A230
recode A230 96/99 = .
replace trennungsjahr = A230 if index == 2004

tab A231
recode A231 96/99 = .
replace trennungsart = A231 if index == 2004


* 2. previous marriage 

tab A233
recode A233 96/99 = .
replace heiratsjahr = A233 if index == 2005

tab A235
recode A235 96/99 = .
replace trennungsjahr = A235 if index == 2005

tab A236
recode A236 96/99 = .
replace trennungsart = A236 if index == 2005


* 3. previous marriage 

tab A199
recode A199 96/99 = .
replace heiratsjahr = A199 if index == 2006

tab A201
recode A201 96/99 = .
replace trennungsjahr = A201 if index == 2006

tab A202
recode A202 96/99 = .
replace trennungsart = A202 if index == 2006

recode trennungsjahr 0 = .
recode trennungsart 0 = .
recode heiratsjahr 0 = .


rename A01 help
drop A* n
rename help A01

save "$widowsghs/processed/intermediate/I2002+_Partner2_LV1.dta", replace

* (3) VERHEIR.,LEBE VON EHEMANN GETRENNT, OHNE NEUE PARTNERIN
* (3) MARRIED, SEPARATED FROM HUSBAND, WITHOUT NEW PARTNER

use "$widowsghs/data/LV1/REC1.DTA", clear

keep if A207 == 3

expand 5
bysort A01: gen n = _n

gen index = 2001 + n

* Husband / wife

tab A210
recode A210 96/99 = .
gen help = A210 if index == 2002
rename help trennungsjahr

tab A211
recode A211 8/9 = .
gen help = A211 if index == 2002
rename help geburtsjahr 


tab A212
recode A212 8/9 = .
gen help = A212 if index == 2002
rename help schulabschluss

tab A213
recode A213 6/9 = .
gen help = A213 if index == 2002
rename help lehre_gewerblich

tab A214
recode A214 6/9 = .
gen help = A214 if index == 2002
rename help lehre_kaufm

tab A215
recode A215 6/9 = .
gen help = A215 if index == 2002
rename help lehre_landwirtschaft

tab A216
recode A216 6/9 = .
gen help = A216 if index == 2002
rename help berufsfachschule

tab A217
recode A217 6/9 = .
gen help = A217 if index == 2002
rename help praktikum

tab A218
recode A218 6/9 = .
gen help = A218 if index == 2002
rename help meisterabschluss

tab A219
recode A219 6/9 = .
gen help = A219 if index == 2002
rename help fachhochschulabschluss

tab A220
recode A220 6/9 = .
gen help = A220 if index == 2002
rename help uniabschluss

tab A221
recode A221 6/9 = .
gen help = A221 if index == 2002
rename help kein_ausbabschluss

tab A225
recode A225 96/99 = .
gen help = A225 if index == 2002
rename help heiratsjahr

tab A300 
recode A300 6/9 = .
gen help = A300 if index == 2002
rename help beruf_erlernt

tab A301
recode A301 96/99 = .
gen help = A301 if index == 2002
rename help beruf_erlernt_prestige 

tab A304
recode A304 6/9 = .
gen help = A304 if index == 2002
rename help beruf_vtrenn

tab A305
recode A305 96/99 = .
gen help = A305 if index == 2002
rename help beruf_vtrenn_prestige

/* Former spouses: Only data on the duration of the marriage is available here */

gen trennungsart = .

* 1. previous marriage

tab A228
recode A228 96/99 = .
replace heiratsjahr = A228 if index == 2003

tab A230
recode A230 96/99 = .
replace trennungsjahr = A230 if index == 2003

tab A231
recode A231 96/99 = .
replace trennungsart = A231 if index == 2003


* 2. previous marriage 

tab A233
recode A233 96/99 = .
replace heiratsjahr = A233 if index == 2004

tab A235
recode A235 96/99 = .
replace trennungsjahr = A235 if index == 2004

tab A236
recode A236 96/99 = .
replace trennungsart = A236 if index == 2004


* 3. previous marriage 

tab A199
recode A199 96/99 = .
replace heiratsjahr = A199 if index == 2005

tab A201
recode A201 96/99 = .
replace trennungsjahr = A201 if index == 2005

tab A202
recode A202 96/99 = .
replace trennungsart = A202 if index == 2005

recode trennungsjahr 0 = .
recode trennungsart 0 = .
recode heiratsjahr 0 = .


rename A01 help
drop A* n
rename help A01

save "$widowsghs/processed/intermediate/I2002+_Partner3_LV1.dta", replace

* (4) GESCHIEDEN, WOHNE OHNE PARTNER
* (4) DIVORCED, LIVING WITHOUT A PARTNER

use "$widowsghs/data/LV1/REC1.DTA", clear

keep if A207 == 4

expand 5
bysort A01: gen n = _n

gen index = 2001 + n

* Former spouse

tab A210
recode A210 96/99 = .
gen help = A210 if index == 2002
rename help trennungsjahr

tab A211
recode A211 8/9 = .
gen help = A211 if index == 2002
rename help geburtsjahr 


tab A212
recode A212 8/9 = .
gen help = A212 if index == 2002
rename help schulabschluss

tab A213
recode A213 6/9 = .
gen help = A213 if index == 2002
rename help lehre_gewerblich

tab A214
recode A214 6/9 = .
gen help = A214 if index == 2002
rename help lehre_kaufm

tab A215
recode A215 6/9 = .
gen help = A215 if index == 2002
rename help lehre_landwirtschaft

tab A216
recode A216 6/9 = .
gen help = A216 if index == 2002
rename help berufsfachschule

tab A217
recode A217 6/9 = .
gen help = A217 if index == 2002
rename help praktikum

tab A218
recode A218 6/9 = .
gen help = A218 if index == 2002
rename help meisterabschluss

tab A219
recode A219 6/9 = .
gen help = A219 if index == 2002
rename help fachhochschulabschluss

tab A220
recode A220 6/9 = .
gen help = A220 if index == 2002
rename help uniabschluss

tab A221
recode A221 6/9 = .
gen help = A221 if index == 2002
rename help kein_ausbabschluss

tab A225
recode A225 96/99 = .
gen help = A225 if index == 2002
rename help heiratsjahr

tab A300 
recode A300 6/9 = .
gen help = A300 if index == 2002
rename help beruf_erlernt

tab A301
recode A301 96/99 = .
gen help = A301 if index == 2002
rename help beruf_erlernt_prestige 

tab A304
recode A304 6/9 = .
gen help = A304 if index == 2002
rename help beruf_vtrenn

tab A305
recode A305 96/99 = .
gen help = A305 if index == 2002
rename help beruf_vtrenn_prestige


/* Former spouses: Only data on the duration of the marriage is available here */

gen trennungsart = .

* 1. previous marriage

tab A228
recode A228 96/99 = .
replace heiratsjahr = A228 if index == 2003

tab A230
recode A230 96/99 = .
replace trennungsjahr = A230 if index == 2003

tab A231
recode A231 96/99 = .
replace trennungsart = A231 if index == 2003


* 2. previous marriage 

tab A233
recode A233 96/99 = .
replace heiratsjahr = A233 if index == 2004

tab A235
recode A235 96/99 = .
replace trennungsjahr = A235 if index == 2004

tab A236
recode A236 96/99 = .
replace trennungsart = A236 if index == 2004


* 3. previous marriage 

tab A199
recode A199 96/99 = .
replace heiratsjahr = A199 if index == 2005

tab A201
recode A201 96/99 = .
replace trennungsjahr = A201 if index == 2005

tab A202
recode A202 96/99 = .
replace trennungsart = A202 if index == 2005

recode trennungsjahr 0 = .
recode trennungsart 0 = .
recode heiratsjahr 0 = .


rename A01 help
drop A* n
rename help A01

save "$widowsghs/processed/intermediate/I2002+_Partner4_LV1.dta", replace

* (5) GESCHIEDEN, WOHNE MIT NEUEM PARTNER ZUSAMMEN
* (5) DIVORCED, LIVING WITH NEW PARTNER

use "$widowsghs/data/LV1/REC1.DTA", clear

keep if A207 == 5

expand 5
bysort A01: gen n = _n

gen index = 2001 + n

* Former spouse

gen status_spouse = A237 if index == 2002
recode status_spouse 0 = .
recode status_spouse 6/9 = .
label var status_spouse "Status des aktuellen Partners"

tab A330
recode A330 96/99 = .
gen help = A330 if index == 2002
rename help zusammenjahr 

tab A238
recode A238 96/99 = .
gen help = A238 if index == 2002
rename help geburtsjahr

tab A239
recode A239 8/9 = .
gen help = A239 if index == 2002
rename help schulabschluss

tab A240
recode A240 6/9 = .
gen help = A240 if index == 2002
rename help lehre_gewerblich

tab A241
recode A241 6/9 = .
gen help = A241 if index == 2002
rename help lehre_kaufm

tab A242
recode A242 6/9 = .
gen help = A242 if index == 2002
rename help lehre_landwirtschaft

tab A243
recode A243 6/9 = .
gen help = A243 if index == 2002
rename help berufsfachschule

tab A244
recode A244 6/9 = .
gen help = A244 if index == 2002
rename help praktikum

tab A245
recode A245 6/9 = .
gen help = A245 if index == 2002
rename help meisterabschluss

tab A246
recode A246 6/9 = .
gen help = A246 if index == 2002
rename help fachhochschulabschluss

tab A247
recode A247 6/9 = .
gen help = A247 if index == 2002
rename help uniabschluss

tab A248
recode A248 6/9 = .
gen help = A248 if index == 2002
rename help kein_ausbabschluss

* Erlernter Beruf & Beruf vor Zusammenleben

tab A300 
recode A300 6/9 = .
gen help = A300 if index == 2002
rename help beruf_erlernt

tab A301
recode A301 96/99 = .
gen help = A301 if index == 2002
rename help beruf_erlernt_prestige 

tab A308
recode A308 6/9 = .
replace A308 = . if status_spouse != 3 & status_spouse != 4 /* Nur für Ehefrau/Partnerin definiert */  
gen help = A308 if index == 2002
rename help beruf_vehe	

tab A309
recode A309 96/99 = .
replace A309 = . if status_spouse != 3 & status_spouse != 4 /* Nur für Ehefrau/Partnerin definiert */  
gen help = A309 if index == 2002
rename help beruf_vehe_prestige	

* Former spouse

tab A210
recode A210 96/99 = .
gen help = A210 if index == 2003
rename help trennungsjahr

tab A211
recode A211 8/9 = .
replace geburtsjahr = A211 if index == 2003

tab A212
recode A212 8/9 = .
replace schulabschluss = A212 if index == 2003

tab A213
recode A213 6/9 = .
replace lehre_gewerblich = A213 if index == 2003

tab A214
recode A214 6/9 = .
replace lehre_kaufm = A214 if index == 2003

tab A215
recode A215 6/9 = .
replace lehre_landwirtschaft = A215 if index == 2003

tab A216
recode A216 6/9 = .
replace berufsfachschule = A216 if index == 2003

tab A217
recode A217 6/9 = .
replace praktikum = A217 if index == 2003

tab A218
recode A218 6/9 = .
replace meisterabschluss = A218 if index == 2003

tab A219
recode A219 6/9 = .
replace fachhochschulabschluss = A219 if index == 2003

tab A220
recode A220 6/9 = .
replace uniabschluss = A220 if index == 2003

tab A221
recode A221 6/9 = .
replace kein_ausbabschluss = A221 if index == 2003

gen heiratsjahr = .
tab A225
recode A225 96/99 = .
replace heiratsjahr = A225 if index == 2003

tab A296 
recode A296 6/9 = .
replace beruf_erlernt = A296 if index == 2003

tab A297
recode A297 96/99 = .
replace beruf_erlernt_prestige = A297 if index == 2003

tab A304
recode A304 6/9 = .
gen help = A304 if index == 2003
rename help beruf_vtrenn

tab A305
recode A305 96/99 = .
gen help = A305 if index == 2003
rename help beruf_vtrenn_prestige

/* Former spouses: Only data on the duration of the marriage is available here */

gen trennungsart = .

* 1. previous marriage

tab A228
recode A228 96/99 = .
replace heiratsjahr = A228 if index == 2004

tab A230
recode A230 96/99 = .
replace trennungsjahr = A230 if index == 2004

tab A231
recode A231 96/99 = .
replace trennungsart = A231 if index == 2004


* 2. previous marriage 

tab A233
recode A233 96/99 = .
replace heiratsjahr = A233 if index == 2005

tab A235
recode A235 96/99 = .
replace trennungsjahr = A235 if index == 2005

tab A236
recode A236 96/99 = .
replace trennungsart = A236 if index == 2005


* 3. previous marriage 

tab A199
recode A199 96/99 = .
replace heiratsjahr = A199 if index == 2006

tab A201
recode A201 96/99 = .
replace trennungsjahr = A201 if index == 2006

tab A202
recode A202 96/99 = .
replace trennungsart = A202 if index == 2006

recode trennungsjahr 0 = .
recode trennungsart 0 = .
recode heiratsjahr 0 = .

rename A01 help
drop A* n
rename help A01

save "$widowsghs/processed/intermediate/I2002+_Partner5_LV1.dta", replace

*(6) VERWITWET, WOHNE OHNE PARTNER
*(6) WIDOWED, LIVING WITHOUT A PARTNER

use "$widowsghs/data/LV1/REC1.DTA", clear

keep if A207 == 6

expand 5
bysort A01: gen n = _n

gen index = 2001 + n

* Former spouse

tab A210
recode A210 96/99 = .
gen help = A210 if index == 2002
rename help trennungsjahr

tab A211
recode A211 8/9 = .
gen help = A211 if index == 2002
rename help geburtsjahr 


tab A212
recode A212 8/9 = .
gen help = A212 if index == 2002
rename help schulabschluss

tab A213
recode A213 6/9 = .
gen help = A213 if index == 2002
rename help lehre_gewerblich

tab A214
recode A214 6/9 = .
gen help = A214 if index == 2002
rename help lehre_kaufm

tab A215
recode A215 6/9 = .
gen help = A215 if index == 2002
rename help lehre_landwirtschaft

tab A216
recode A216 6/9 = .
gen help = A216 if index == 2002
rename help berufsfachschule

tab A217
recode A217 6/9 = .
gen help = A217 if index == 2002
rename help praktikum

tab A218
recode A218 6/9 = .
gen help = A218 if index == 2002
rename help meisterabschluss

tab A219
recode A219 6/9 = .
gen help = A219 if index == 2002
rename help fachhochschulabschluss

tab A220
recode A220 6/9 = .
gen help = A220 if index == 2002
rename help uniabschluss

tab A221
recode A221 6/9 = .
gen help = A221 if index == 2002
rename help kein_ausbabschluss

tab A225
recode A225 96/99 = .
gen help = A225 if index == 2002
rename help heiratsjahr

tab A300 
recode A300 6/9 = .
gen help = A300 if index == 2002
rename help beruf_erlernt

tab A301
recode A301 96/99 = .
gen help = A301 if index == 2002
rename help beruf_erlernt_prestige 

tab A304
recode A304 6/9 = .
gen help = A304 if index == 2002
rename help beruf_vtrenn

tab A305
recode A305 96/99 = .
gen help = A305 if index == 2002
rename help beruf_vtrenn_prestige

/* Other previous spouses: Only data on the duration of the marriage is available here */

gen trennungsart = .

* 1. previous marriage

tab A228
recode A228 96/99 = .
replace heiratsjahr = A228 if index == 2003

tab A230
recode A230 96/99 = .
replace trennungsjahr = A230 if index == 2003

tab A231
recode A231 96/99 = .
replace trennungsart = A231 if index == 2003


* 2. previous marriage 

tab A233
recode A233 96/99 = .
replace heiratsjahr = A233 if index == 2004

tab A235
recode A235 96/99 = .
replace trennungsjahr = A235 if index == 2004

tab A236
recode A236 96/99 = .
replace trennungsart = A236 if index == 2004


* 3. previous marriage 

tab A199
recode A199 96/99 = .
replace heiratsjahr = A199 if index == 2005

tab A201
recode A201 96/99 = .
replace trennungsjahr = A201 if index == 2005

tab A202
recode A202 96/99 = .
replace trennungsart = A202 if index == 2005

recode trennungsjahr 0 = .
recode trennungsart 0 = .
recode heiratsjahr 0 = .


rename A01 help
drop A* n
rename help A01

save "$widowsghs/processed/intermediate/I2002+_Partner6_LV1.dta", replace

* (7) VERWITWET, WOHNE MIT PARTNER ZUSAMMEN
* (7) WIDOWED, LIVING WITH PARTNER

use "$widowsghs/data/LV1/REC1.DTA", clear

keep if A207 == 7

expand 5
bysort A01: gen n = _n

gen index = 2001 + n

* Former spouse

gen status_spouse = A237 if index == 2002
recode status_spouse 0 = .
recode status_spouse 6/9 = .
label var status_spouse "Status des aktuellen Partners"

tab A330
recode A330 96/99 = .
gen help = A330 if index == 2002
rename help zusammenjahr 

tab A238
recode A238 96/99 = .
gen help = A238 if index == 2002
rename help geburtsjahr

tab A239
recode A239 8/9 = .
gen help = A239 if index == 2002
rename help schulabschluss

tab A240
recode A240 6/9 = .
gen help = A240 if index == 2002
rename help lehre_gewerblich

tab A241
recode A241 6/9 = .
gen help = A241 if index == 2002
rename help lehre_kaufm

tab A242
recode A242 6/9 = .
gen help = A242 if index == 2002
rename help lehre_landwirtschaft

tab A243
recode A243 6/9 = .
gen help = A243 if index == 2002
rename help berufsfachschule

tab A244
recode A244 6/9 = .
gen help = A244 if index == 2002
rename help praktikum

tab A245
recode A245 6/9 = .
gen help = A245 if index == 2002
rename help meisterabschluss

tab A246
recode A246 6/9 = .
gen help = A246 if index == 2002
rename help fachhochschulabschluss

tab A247
recode A247 6/9 = .
gen help = A247 if index == 2002
rename help uniabschluss

tab A248
recode A248 6/9 = .
gen help = A248 if index == 2002
rename help kein_ausbabschluss

tab A300 
recode A300 6/9 = .
gen help = A300 if index == 2002
rename help beruf_erlernt

tab A301
recode A301 96/99 = .
gen help = A301 if index == 2002
rename help beruf_erlernt_prestige 

tab A308
recode A308 6/9 = .
replace A308 = . if status_spouse != 3 & status_spouse != 4 /* Only defined for wife/female partner */ 
gen help = A308 if index == 2002
rename help beruf_vehe	

tab A309
recode A309 96/99 = .
replace A309 = . if status_spouse != 3 & status_spouse != 4 /* Only defined for wife/female partner */  
gen help = A309 if index == 2002
rename help beruf_vehe_prestige	

* Former spouse

tab A210
recode A210 96/99 = .
gen help = A210 if index == 2003
rename help trennungsjahr

tab A211
recode A211 8/9 = .
replace geburtsjahr = A211 if index == 2003

tab A212
recode A212 8/9 = .
replace schulabschluss = A212 if index == 2003

tab A213
recode A213 6/9 = .
replace lehre_gewerblich = A213 if index == 2003

tab A214
recode A214 6/9 = .
replace lehre_kaufm = A214 if index == 2003

tab A215
recode A215 6/9 = .
replace lehre_landwirtschaft = A215 if index == 2003

tab A216
recode A216 6/9 = .
replace berufsfachschule = A216 if index == 2003

tab A217
recode A217 6/9 = .
replace praktikum = A217 if index == 2003

tab A218
recode A218 6/9 = .
replace meisterabschluss = A218 if index == 2003

tab A219
recode A219 6/9 = .
replace fachhochschulabschluss = A219 if index == 2003

tab A220
recode A220 6/9 = .
replace uniabschluss = A220 if index == 2003

tab A221
recode A221 6/9 = .
replace kein_ausbabschluss = A221 if index == 2003

gen heiratsjahr = .
tab A225
recode A225 96/99 = .
replace heiratsjahr = A225 if index == 2003

tab A296 
recode A296 6/9 = .
replace beruf_erlernt = A296 if index == 2003

tab A297
recode A297 96/99 = .
replace beruf_erlernt = A297 if index == 2003

tab A304
recode A304 6/9 = .
gen help = A304 if index == 2003
rename help beruf_vtrenn

tab A305
recode A305 96/99 = .
gen help = A305 if index == 2003
rename help beruf_vtrenn_prestige

/* Former spouses: Only data on the duration of the marriage is available here */

gen trennungsart = .

* 1. previous marriage

tab A228
recode A228 96/99 = .
replace heiratsjahr = A228 if index == 2004

tab A230
recode A230 96/99 = .
replace trennungsjahr = A230 if index == 2004

tab A231
recode A231 96/99 = .
replace trennungsart = A231 if index == 2004


* 2. previous marriage 

tab A233
recode A233 96/99 = .
replace heiratsjahr = A233 if index == 2005

tab A235
recode A235 96/99 = .
replace trennungsjahr = A235 if index == 2005

tab A236
recode A236 96/99 = .
replace trennungsart = A236 if index == 2005


* 3. previous marriage 

tab A199
recode A199 96/99 = .
replace heiratsjahr = A199 if index == 2006

tab A201
recode A201 96/99 = .
replace trennungsjahr = A201 if index == 2006

tab A202
recode A202 96/99 = .
replace trennungsart = A202 if index == 2006

recode trennungsjahr 0 = .
recode trennungsart 0 = .
recode heiratsjahr 0 = .

rename A01 help
drop A* n
rename help A01

save "$widowsghs/processed/intermediate/I2002+_Partner7_LV1.dta", replace

* (9) LEDIG, WOHNE MIT MEINEM PARTNER ZUSAMMEN
* (9) SINGLE, LIVING WITH MY PARTNER

use "$widowsghs/data/LV1/REC1.DTA", clear

keep if A207 == 9

expand 5
bysort A01: gen n = _n

gen index = 2001 + n

* Former spouse

gen status_spouse = A237 if index == 2002
recode status_spouse 0 = .
recode status_spouse 6/9 = .
label var status_spouse "Status des aktuellen Partners"

tab A330
recode A330 96/99 = .
gen help = A330 if index == 2002
rename help zusammenjahr 

tab A238
recode A238 96/99 = .
gen help = A238 if index == 2002
rename help geburtsjahr

tab A239
recode A239 8/9 = .
gen help = A239 if index == 2002
rename help schulabschluss

tab A240
recode A240 6/9 = .
gen help = A240 if index == 2002
rename help lehre_gewerblich

tab A241
recode A241 6/9 = .
gen help = A241 if index == 2002
rename help lehre_kaufm

tab A242
recode A242 6/9 = .
gen help = A242 if index == 2002
rename help lehre_landwirtschaft

tab A243
recode A243 6/9 = .
gen help = A243 if index == 2002
rename help berufsfachschule

tab A244
recode A244 6/9 = .
gen help = A244 if index == 2002
rename help praktikum

tab A245
recode A245 6/9 = .
gen help = A245 if index == 2002
rename help meisterabschluss

tab A246
recode A246 6/9 = .
gen help = A246 if index == 2002
rename help fachhochschulabschluss

tab A247
recode A247 6/9 = .
gen help = A247 if index == 2002
rename help uniabschluss

tab A248
recode A248 6/9 = .
gen help = A248 if index == 2002
rename help kein_ausbabschluss

tab A296 
recode A296 6/9 = .
gen help = A296 if index == 2002
rename help beruf_erlernt

tab A297
recode A297 96/99 = .
gen help = A297 if index == 2002
rename help beruf_erlernt_prestige 

tab A308
recode A308 6/9 = .
replace A308 = . if status_spouse != 3 & status_spouse != 4 /* Only defined for wife/female partner */  
gen help = A308 if index == 2002
rename help beruf_vehe	

tab A309
recode A309 96/99 = .
replace A309 = . if status_spouse != 3 & status_spouse != 4 /* Only defined for wife/female partner */  
gen help = A309 if index == 2002
rename help beruf_vehe_prestige	

rename A01 help
drop A* n
rename help A01

save "$widowsghs/processed/intermediate/I2002+_Partner9_LV1.dta", replace




***************************************************************************
* Information from REC13 JOBPART => Employment history of current spouse/partner
***************************************************************************

use "$widowsghs/data/LV1/JOBPART.dta", clear

* Recode prestige to zero (if unemployed, house wife,...) or missing (if occupation not specified)

tab N03
recode N03 8/9 = .
replace N04 = . if N03 == . | N03 == .b
replace N04 = 0 if N03 == 3 | N03 == 5 | N03 == 6 | N03 == 7 /* Job seeker, in training, housewife, retired */
recode N04 0 = . if N03 == 4 /* Occupation undefined */

bysort A01: egen beruf_prestige_max = max(N04)
label var beruf_prestige_max "Highest prestige value achieved in the course of employment"

* Save

gen index = 2002

keep if N01 == 1

keep A01 beruf* index

compress

sort A01 index
save "$widowsghs/processed/intermediate/I2002+_REC13_LV1.dta", replace


* Merging the data on partners

use "$widowsghs/processed/intermediate/I2002+_Partner9_LV1.dta", clear 
forvalues t = 1/7 {
append using "$widowsghs/processed/intermediate/I2002+_Partner`t'_LV1.dta"
}

sort A01 index
merge m:m A01 index using "$widowsghs/processed/intermediate/I2002+_REC13_LV1.dta"
drop _merge

* Generate data on vocational training, tertiary education

gen berufsausbildung = .
replace berufsausbildung = 9 if kein_ausbabschluss == 1 | (kein_ausbabschluss == 0 & praktikum == 1)
replace berufsausbildung = 3 if lehre_landwirtschaft == 1
replace berufsausbildung = 1 if lehre_gewerblich == 1
replace berufsausbildung = 2 if lehre_kaufm == 1
replace berufsausbildung = 4 if berufsfachschule == 1
replace berufsausbildung = 6 if meisterabschluss == 1
replace berufsausbildung = 7 if fachhochschulabschluss == 1
replace berufsausbildung = 8 if uniabschluss == 1 

tab berufsausbildung

drop lehre_gewerblich-kein_ausbabschluss

* Recode year variables

recode geburtsjahr 0 = .
recode geburtsjahr 97/99 = .
replace geburtsjahr = geburtsjahr + 1900

replace heiratsjahr = heiratsjahr + 1900
replace trennungsjahr = trennungsjahr + 1900
replace zusammenjahr = zusammenjahr + 1900


* Reshape to wide format

foreach x in status_spouse zusammenjahr geburtsjahr schulabschluss beruf_erlernt beruf_erlernt_prestige beruf_vehe beruf_vehe_prestige heiratsjahr trennungsart trennungsjahr beruf_vtrenn beruf_vtrenn_prestige beruf_prestige_max berufsausbildung{
rename `x' `x'_
} 

reshape wide status_spouse_ zusammenjahr_ geburtsjahr_ schulabschluss_ beruf_erlernt_ beruf_erlernt_prestige_ beruf_vehe_ beruf_vehe_prestige_ heiratsjahr_ trennungsart_ trennungsjahr_ beruf_vtrenn_ beruf_vtrenn_prestige_ beruf_prestige_max_ berufsausbildung_, i(A01) j(index)

* Delete variables without observations
forvalues i=2002/2006{
foreach x in status_spouse_ zusammenjahr_ geburtsjahr_ schulabschluss_ beruf_erlernt_ beruf_erlernt_prestige_ beruf_vehe_ beruf_vehe_prestige_ heiratsjahr_ trennungsart_ trennungsjahr_ beruf_vtrenn_ beruf_vtrenn_prestige_ beruf_prestige_max_ berufsausbildung_{
egen help = count(`x'`i')
if help == 0 {
drop `x'`i'
}
drop help
}
}


* Labels

label var status_spouse_2002 "A237: Status of the partnership"
label define status 1 "Ehemann" 2 "Partner" 3 "Ehefrau" 4 "Partnerin"
label values status_spouse_2002 status

label var zusammenjahr_2002 "A330: First year of cohabitation with partner"

label var geburtsjahr_2002 "Year of birth spouse/partner 2002"
label var geburtsjahr_2003 "Year of birth spouse 2003"

label var schulabschluss_2002 "Highest school-leaving qualification spouse/partner 2002"
label var schulabschluss_2003 "Highest school-leaving qualification spouse 2003"
label define schule 1 "Sonderschulabschluss" 2 "Volks-HS ohne Abschluss" 3 "Volks-HS mit Abschluss" 4 "Mittlere Reife" 5 "Fachhochschulreife" 6 "Abitur"
label values schulabschluss_2002 schule
label values schulabschluss_2003 schule

label var beruf_erlernt_2002 "Profession learned spouse/partner 2002 (ISCO)"
label var beruf_erlernt_2003 "Profession learned spouse 2003 (ISCO)"

label var beruf_erlernt_prestige_2002 "Prestige of the profession learned spouse/partner 2002 (Treiman)"
label var beruf_erlernt_prestige_2003 "Prestige of the profession learned spouse 2003 (Treiman)"

label var beruf_vtrenn_2002 "Occupation before separation spouse/partner 2002 (ISCO)"
label var beruf_vtrenn_2003 "Occupation before separation spouse 2003 (ISCO)"

label var beruf_vtrenn_prestige_2002 "Prestige of the profession before separation spouse/partner 2002 (Treiman)"
label var beruf_vtrenn_prestige_2003 "Prestige of the profession before separation spouse 2003 (Treiman)"

label var berufsausbildung_2002 "Highest educational qualification spouse/partner 2002"
label var berufsausbildung_2003 "Highest educational qualification spouse 2003"
label values berufsausbildung_2002 berufsausbl
label values berufsausbildung_2003 berufsausbl

label var beruf_vehe_2002 "Occupation of spouse/partner 2002 before marriage (ISCO)"
label var beruf_vehe_prestige_2002 "Prestige of the profession practiced by the spouse/partner 2002 before the marriage (Treiman)"
label var beruf_prestige_max_2002 "Maximum prestige of spouse/partner 2002 in the course of employment (Treiman)"


forvalues i=2002/2005{
label var heiratsjahr_`i' "Year of marriage spouse/partner `i'"
label var trennungsjahr_`i' "Year of separation spouse/partner `i'"
}

label define trennung 1 "Spouse deceased" 2 "Divorced"
forvalues i=2003/2005{
label var trennungsart_`i' "Type of separation spouse/partner `i'"
label values trennungsart_`i' trennung
}

sort A01

save "$widowsghs/processed/intermediate/LVIPa_CS_I2002_6.dta", replace


***********************************************
* Siblings of the index generation -- REC04
***********************************************

* Attention: Vocational training is only coded from 1-8 for siblings (the category university of applied sciences is missing) 
* Here: Set category 9 (which should not exist) to "no training"

forvalues t = 1/12 {

use "$widowsghs/data/LV1/GESCHWIS.DTA", clear
keep if D01 == `t'
gen index = 2010 + `t'

tab D02
recode D02 7/9 = .
rename D02 geschlecht

tab D03
recode D03 97/99 = .
rename D03 geburtsjahr
replace geburtsjahr = geburtsjahr + 1900

tab D04
recode D04 97/99 = .
recode D04 0 = .
rename D04 todesjahr
replace todesjahr = todesjahr + 1900

tab D05
recode D05 7/9 = .
recode D05 0 = .
rename D05 schulabschluss

tab D06
recode D06 97/99 = .
recode D06 0 = .
recode D06 10 = .
recode D06 8 = 9
recode D06 7 = 8
rename D06 berufsausbildung

tab D07
recode D07 0 = .
recode D07 7/9 = .
rename D07 familienstand

tab D09
recode D09 97/99 = .
rename D09 kinder_anzahl

tab D11
recode D11 97/99 = .
rename D11 stellung_beruf_jetzt

drop D* CD*

save "$widowsghs/processed/intermediate/I2011+_Geschwister`t'_LV1.dta", replace
}

clear
forvalues t = 1/12 {
append using "$widowsghs/processed/intermediate/I2011+_Geschwister`t'_LV1.dta"
}


* Reshape to wide format

foreach x in geschlecht geburtsjahr todesjahr schulabschluss berufsausbildung familienstand kinder_anzahl stellung_beruf_jetzt {
rename `x' `x'_
} 

reshape wide geschlecht_ geburtsjahr_ todesjahr_ schulabschluss_ berufsausbildung_ familienstand_ kinder_anzahl_ stellung_beruf_jetzt_, i(A01) j(index)

* Label 
forvalues i = 2011/2022{
label var geschlecht_`i' "Gender sibling `i'"
label var geburtsjahr_`i' "Year of birth sibling `i'"
label var todesjahr_`i' "Year of death sibling `i'"
label var schulabschluss_`i' "Highest school-leaving qualification sibling `i'"
label var berufsausbildung_`i' "Highest educational qualification sibling `i'"
label values berufsausbildung_`i' berufsausbl

label var familienstand_`i' "Marital status sibling `i'"
label var kinder_anzahl_`i' "Number of children sibling `i'"
label var stellung_beruf_jetzt_`i' "Current professional position sibling `i'"
}

* Save

sort A01

save "$widowsghs/processed/intermediate/LVIPa_CS_I2011_22.dta", replace

***********************************************
* Mother of the index person
***********************************************

****************************************************
* RECORD 01 - Information on the biological mother -
****************************************************

use "$widowsghs/data/LV1/REC1.DTA", clear

tab A142
recode A142 996/999 = .
rename A142 geburtsjahr
replace geburtsjahr = geburtsjahr + 1000

tab A144
recode A144 0 = .
recode A144 96/99 = .
rename A144 todesjahr
replace todesjahr = todesjahr + 1900

tab A146
recode A146 8/9 = .
rename A146 schulabschluss

tab A147
recode A147 6/9 = .
rename A147 lehre_gewerblich

tab A148
recode A148 6/9 = .
rename A148 lehre_kaufm

tab A149
recode A149 6/9 = .
rename A149 lehre_landwirtschaft

tab A150
recode A150 6/9 = .
rename A150 berufsfachschule

tab A151
recode A151 6/9 = .
rename A151 praktikum

tab A152
recode A152 6/9 = .
rename A152 meisterabschluss

tab A153
recode A153 6/9 = .
rename A153 fachhochschulabschluss

tab A154
recode A154 6/9 = .
rename A154 uniabschluss

tab A155
recode A155 6/9 = .
rename A155 kein_ausbabschluss

gen berufsausbildung = .
replace berufsausbildung = 9 if kein_ausbabschluss == 1 | (kein_ausbabschluss == 0 & praktikum == 1)
replace berufsausbildung = 3 if lehre_landwirtschaft == 1
replace berufsausbildung = 1 if lehre_gewerblich == 1
replace berufsausbildung = 2 if lehre_kaufm == 1
replace berufsausbildung = 4 if berufsfachschule == 1
replace berufsausbildung = 6 if meisterabschluss == 1
replace berufsausbildung = 7 if fachhochschulabschluss == 1
replace berufsausbildung = 8 if uniabschluss == 1 

tab berufsausbildung

* Drop superfluous variables
keep A01 geburtsjahr todesjahr schulabschluss berufsausbildung  

* Rename variables with 1002 ending (index for mother)
foreach x in geburtsjahr todesjahr schulabschluss berufsausbildung {
rename `x' `x'_1002
} 

* Label 
label var geburtsjahr_1002 "Year of birth mother" 
label var todesjahr_1002 "Year of death mother"

label var berufsausbildung_1002 "Highest educational qualification mother"
label values berufsausbildung_1002 berufsausbl

label var schulabschluss_1002 "Highest school-leaving qualification mother"


* Save
sort A01
save "$widowsghs/processed/intermediate/LVIPa_CS_I1002.dta", replace


***********************************************
* Father of the index generation
***********************************************

************************************************
* RECORD 01 - Details of the biological father -
*************************************************

use "$widowsghs/data/LV1/REC1.DTA", clear

tab A169
recode A169 996/999 = .
rename A169 geburtsjahr
replace geburtsjahr = geburtsjahr + 1000

tab A171
recode A171 0 = .
recode A171 96/99 = .
rename A171 todesjahr
replace todesjahr = todesjahr + 1900

tab A173
recode A173 8/9 = .
rename A173 schulabschluss

tab A174
recode A174 6/9 = .
rename A174 lehre_gewerblich

tab A175
recode A175 6/9 = .
rename A175 lehre_kaufm

tab A176
recode A176 6/9 = .
rename A176 lehre_landwirtschaft

tab A177
recode A177 6/9 = .
rename A177 berufsfachschule

tab A178
recode A178 6/9 = .
rename A178 praktikum

tab A179
recode A179 6/9 = .
rename A179 meisterabschluss

tab A180
recode A180 6/9 = .
rename A180 fachhochschulabschluss

tab A181
recode A181 6/9 = .
rename A181 uniabschluss

tab A182
recode A182 6/9 = .
rename A182 kein_ausbabschluss

gen berufsausbildung = .
replace berufsausbildung = 9 if kein_ausbabschluss == 1 | (kein_ausbabschluss == 0 & praktikum == 1)
replace berufsausbildung = 3 if lehre_landwirtschaft == 1
replace berufsausbildung = 1 if lehre_gewerblich == 1
replace berufsausbildung = 2 if lehre_kaufm == 1
replace berufsausbildung = 4 if berufsfachschule == 1
replace berufsausbildung = 6 if meisterabschluss == 1
replace berufsausbildung = 7 if fachhochschulabschluss == 1
replace berufsausbildung = 8 if uniabschluss == 1 

tab berufsausbildung

* Learned profession of the father

tab A268
recode A268 6/9 = .
rename A268 beruf_erlernt

tab A269
recode A269 96/99 = .
rename A269 beruf_erlernt_prestige


* Father's occupation when IP was 15 years old: If father was not working when IP was 15 years old, occupation before IP was 15 years old

tab A316
recode A316 7/9 = .
rename A316 beruf_ip15j_jn 

tab A272
recode A272 996/999 = .
rename A272 beruf_ip15j 

tab A273 
recode A273 96/99 = .
rename A273 beruf_ip15j_prestige 

tab A317
recode A317 96/99 = .
rename A317 stellung_beruf_ip15j

* Current/last profession of the father

tab A276
recode A276 996/999 = .
rename A276 beruf_vRuhe 

tab A277
recode A277 96/99 = .
rename A277 beruf_vRuhe_prestige

tab A318
recode A318 96/99 = .
rename A318 stellung_beruf_vRuhe


* Drop superfluous variables
keep A01 geburtsjahr todesjahr schulabschluss beruf_* stellung* berufsausbildung
sort A01

* Rename variables with 1001 ending (for father)

foreach x in geburtsjahr todesjahr schulabschluss beruf_erlernt beruf_erlernt_prestige beruf_ip15j beruf_ip15j_prestige beruf_vRuhe beruf_vRuhe_prestige beruf_ip15j_jn stellung_beruf_ip15j stellung_beruf_vRuhe berufsausbildung {
rename `x' `x'_1001
} 

* Labels
label var geburtsjahr_1001 "Year of birth father" 
label var todesjahr_1001 "Year of death mother"

label var berufsausbildung_1001 "Highest educational qualification father"
label values berufsausbildung_1001 berufsausbl

label var schulabschluss_1001 "Highest school-leaving qualification father" 


* Save
sort A01
save "$widowsghs/processed/intermediate/LVIPa_CS_I1001.dta", replace



**************************************************
* RECORD 14 KINDER -- Kinder der Indexgeneration
**************************************************

*** Information not included in dataset

***********************************************
* Merge cross-sectional data
***********************************************

clear

use "$widowsghs/processed/intermediate/LVIPa_CS_I2001.dta"
merge 1:1 A01 using "$widowsghs/processed/intermediate/LVIPa_CS_I2002_6.dta"
drop _merge
sort A01
merge 1:1 A01 using "$widowsghs/processed/intermediate/LVIPa_CS_I2011_22.dta"
drop _merge
merge 1:1 A01 using "$widowsghs/processed/intermediate/LVIPa_CS_I1001.dta"
drop _merge
merge 1:1 A01 using "$widowsghs/processed/intermediate/LVIPa_CS_I1002.dta"
drop _merge

compress
save "$widowsghs/processed/intermediate/LVIPa_CS_combined.dta", replace
